package com.sjy.project.fym.lab.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.validation.Validator;

import com.sjy.common.exception.ServiceException;
import com.sjy.common.utils.DateUtils;
import com.sjy.common.utils.GPSUtil;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.bean.BeanValidators;
import com.sjy.common.utils.security.ShiroUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjy.project.fym.lab.mapper.FyLabMapper;
import com.sjy.project.fym.lab.domain.FyLab;
import com.sjy.project.fym.lab.service.IFyLabService;
import com.sjy.project.system.user.service.UserServiceImpl;
import com.sjy.common.utils.text.Convert;

/**
 * 实验室信息Service业务层处理
 * 
 * @author ljb
 * @date 2022-05-15
 */
@Service
public class FyLabServiceImpl implements IFyLabService 
{
	private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private FyLabMapper fyLabMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询实验室信息
     * 
     * @param labId 实验室信息主键
     * @return 实验室信息
     */
    @Override
    public FyLab selectFyLabByLabId(Long labId)
    {
        return fyLabMapper.selectFyLabByLabId(labId);
    }

    /**
     * 查询实验室信息列表
     * 
     * @param fyLab 实验室信息
     * @return 实验室信息
     */
    @Override
    public List<FyLab> selectFyLabList(FyLab fyLab)
    {
        return fyLabMapper.selectFyLabList(fyLab);
    }

    /**
     * 新增实验室信息
     * 
     * @param fyLab 实验室信息
     * @return 结果
     */
    @Override
    public int insertFyLab(FyLab fyLab)
    {
        fyLab.setCreateTime(DateUtils.getNowDate());
        return fyLabMapper.insertFyLab(fyLab);
    }

    /**
     * 修改实验室信息
     * 
     * @param fyLab 实验室信息
     * @return 结果
     */
    @Override
    public int updateFyLab(FyLab fyLab)
    {
        fyLab.setUpdateTime(DateUtils.getNowDate());
        return fyLabMapper.updateFyLab(fyLab);
    }

    /**
     * 批量删除实验室信息
     * 
     * @param labIds 需要删除的实验室信息主键
     * @return 结果
     */
    @Override
    public int deleteFyLabByLabIds(String labIds)
    {
        return fyLabMapper.deleteFyLabByLabIds(Convert.toStrArray(labIds));
    }

    /**
     * 删除实验室信息信息
     * 
     * @param labId 实验室信息主键
     * @return 结果
     */
    @Override
    public int deleteFyLabByLabId(Long labId)
    {
        return fyLabMapper.deleteFyLabByLabId(labId);
    }
    
    /**
     * 导入实验室数据
     * 
     * @param labList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importLab(List<FyLab> labList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(labList) || labList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        String operName = ShiroUtils.getLoginName();
        for (FyLab lab : labList)
        {
            try
            {
                // 验证是否存在这个防疫点
                FyLab u = fyLabMapper.selectLabByLabName(lab.getLabName());
                
                //更新lngSjy latSjy 到 lng lat
                double[] lnglat = GPSUtil.bd09_To_gps84(new Double(lab.getLatSjy()+""), new Double(lab.getLngSjy()+""));
                lab.setLng(new BigDecimal(lnglat[1]));
                lab.setLat(new BigDecimal(lnglat[0]));
                
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, lab);
                    lab.setCreateBy(operName);
                    this.insertFyLab(lab);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、实验室 " + lab.getLabName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, lab);
                    lab.setUpdateBy(operName);
                    lab.setLabId(u.getLabId());
                    this.updateFyLab(lab);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、实验室 " + lab.getLabName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、实验室 " + lab.getLabName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、实验室 " + lab.getLabName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
    
}
